import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:common_utils/common_utils.dart';
import 'dart:ui';
// 自定义模块
import '../module/chat_appbar.dart';

import '../components/msg_item.dart';

class SizeItems {
  double width;
  double height;
  SizeItems({this.width = 0,this.height = 0});
}

class UserChat extends StatefulWidget {
  UserChat({Key key, this.userId}) : super(key: key);
  final int userId;
  @override
  _UserChatState createState() => _UserChatState();
}

class _UserChatState extends State<UserChat> {
  ScrollController _scrollController;
  final _inputText = TextEditingController();
  RenderObject renderObject;
  SizeItems maxSize;
  SizeItems minSize;
  double _keyboard;
  @override
    void initState() {
      super.initState();
      _scrollController = new ScrollController();
      maxSize = SizeItems(
        width: MediaQueryData.fromWindow(window).size.width,
        height: MediaQueryData.fromWindow(window).size.height - MediaQueryData.fromWindow(window).padding.top
      );
      print(maxSize.width);
      print(maxSize.height);
      minSize = SizeItems();
      _keyboard = ScreenUtil().setWidth(580);
      renderObject = null;
      _inputText.addListener((){
        // if (renderObject != null && renderObject.semanticBounds != null) {
        //   var xiangcha = renderObject.semanticBounds.size.height - maxSize.height;
        //   xiangcha = xiangcha.abs();
        //   if (xiangcha > 25) { // 软件盘弹起状态
        //     setState(() {
        //       _keyboard =  maxSize.height -  renderObject.semanticBounds.size.height;
        //     });
        //   }
        // }
        // if (_keyboard != 0.0) {
        //   print('软件盘高度：${_keyboard}');
        // }
      });
    }
    void dispose() {
      _inputText.dispose();
      super.dispose();
    }

  GlobalKey _myKey = new GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          backgroundColor: Color.fromRGBO(246, 247, 249, 1),
          brightness: Brightness.light,
          elevation: 0,
        ),
        preferredSize:  Size.fromHeight(0),
      ),
      body: Builder(
        builder: (context) {
          return Column(
            key: _myKey,
            children: <Widget>[
              ChatAppBar(
                name: 'Yohann-${widget.userId}',
                desc: '用户描述',
                bgColor: Color.fromRGBO(246, 247, 249, 1),
                textColor: Color.fromRGBO(12, 12, 12, 1),
                leftICon: GestureDetector(
                  child: Container(
                    margin: EdgeInsets.all(ScreenUtil().setWidth(20)),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(50.0)
                    ),
                    child: Icon(
                      IconData(0xe622, fontFamily: 'iconfont'),
                      size: ScreenUtil().setWidth(32),
                    )
                  ),
                  onTap: () {
                    print('返回');
                    Navigator.pop(context);
                  },
                ),
                rightICon: GestureDetector(
                  child: Container(
                    margin: EdgeInsets.all(ScreenUtil().setWidth(10)),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(50.0)
                    ),
                    child: Icon(
                      Icons.perm_identity,
                      size: ScreenUtil().setWidth(42),
                    )
                  ),
                  onTap: () {
                    print('查看用户信息');
                  },
                ),
              ),
              Expanded(
                child: Container(
                  color: Color.fromRGBO(242, 243, 246, 1),
                  child: ListView.builder(
                    itemCount: 50,
                    reverse: true,
                    controller: _scrollController,
                    itemBuilder: (BuildContext context, index){
                      return MsgItem(
                        msginfo: {
                          "appid" : "wx742658a0bf1686cb",
                          "content" : "你好",
                          "cs_id" : "1138",
                          "needsend" : "0",
                          "openid" : "oYUe4jnj9K3n4uk0IoDVT1qxm7Jw",
                          "pid" : "1069",
                          "send_time" : "1547175388",
                          "send_type" : index % 2 == 0 ? 1001 : 1002,
                          "type" : "text",
                        }
                      );
                      // ${DateTime.now().millisecondsSinceEpoch}
                    },
                  )
                ),
              ),
              Container(
                height: ScreenUtil().setHeight(150),
                color: Color.fromRGBO(251, 251, 251, 1),
                child: Column(
                  children: <Widget>[
                    Container(
                      padding:EdgeInsets.all(ScreenUtil().setWidth(13)),
                      height: ScreenUtil().setHeight(78),
                      child: Container(
                        padding: EdgeInsets.all(0),
                        height:ScreenUtil().setHeight(52),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                              child: TextField(
                                keyboardAppearance: Brightness.light,
                                onChanged: (text) {
                                  print('弹出键盘时');
                                  setState(() {
                                    renderObject = _myKey.currentContext.findRenderObject();
                                    minSize.width = renderObject.semanticBounds.size.width;
                                    minSize.height = renderObject.semanticBounds.size.height;
                                  });
                                },
                                controller: _inputText,
                                textInputAction: TextInputAction.done,
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.all(0),
                                  border: InputBorder.none
                                ),
                              ),
                            ),
                            GestureDetector(
                              child: Container(
                                alignment: Alignment(0, 0),
                                color: Color.fromRGBO(251, 251, 251, 1),
                                child: Text(
                                  '发送',
                                  style: TextStyle(
                                    color: Color.fromRGBO(4, 143, 249, 1),
                                    fontSize: ScreenUtil().setWidth(28)
                                  ),
                                ),
                                padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(20), 0, ScreenUtil().setWidth(20), ScreenUtil().setWidth(20)),
                              ),
                              onTap: () {
                                // print(MediaQueryData.fromWindow(window));
                                // FocusScope.of(context).requestFocus(FocusNode());
                                // setState(() {
                                //   renderObject = _myKey.currentContext.findRenderObject();
                                // });
                              },
                            )
                          ],
                        ),
                      ),
                    ),
                    Container(
                      height: ScreenUtil().setHeight(72),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            color: Color.fromRGBO(222, 223, 224, 1),
                          )
                        )
                      ),
                      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(10), 0, ScreenUtil().setWidth(10), 0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          Container(
                            width:ScreenUtil().setHeight(72),
                            height:ScreenUtil().setHeight(72),
                            child: GestureDetector(
                              child: Center(
                                child: Icon(IconData(0xe62e, fontFamily: 'iconfont')),
                              ),
                              onTap: (){FocusScope.of(context).requestFocus(FocusNode());},
                            ),
                          ),
                          Container(
                            width:ScreenUtil().setHeight(72),
                            height:ScreenUtil().setHeight(72),
                            child: GestureDetector(
                              child: Center(
                                child: Icon(IconData(0xe632, fontFamily: 'iconfont')),
                              ),
                              onTap: (){
                                print(MediaQueryData.fromWindow(window).padding);
                              },
                            ),
                          ),
                          Container(
                            width:ScreenUtil().setHeight(72),
                            height:ScreenUtil().setHeight(72),
                            child: GestureDetector(
                              child: Center(
                                child: Icon(IconData(0xe614, fontFamily: 'iconfont')),
                              ),
                              onTap: (){},
                            ),
                          ),
                          Container(
                            width:ScreenUtil().setHeight(72),
                            height:ScreenUtil().setHeight(72),
                            child: GestureDetector(
                              child: Center(
                                child: Icon(IconData(0xe68a, fontFamily: 'iconfont')),
                              ),
                              onTap: (){},
                            ),
                          ),
                          Container(
                            width:ScreenUtil().setHeight(72),
                            height:ScreenUtil().setHeight(72),
                            child: GestureDetector(
                              child: Center(
                                child: Icon(IconData(0xe615, fontFamily: 'iconfont')),
                              ),
                              onTap: (){},
                            ),
                          ),
                          Container(
                            width:ScreenUtil().setHeight(72),
                            height:ScreenUtil().setHeight(72),
                            child: GestureDetector(
                              child: Center(
                                child: Icon(IconData(0xe693, fontFamily: 'iconfont')),
                              ),
                              onTap: (){},
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              )
            ],
          );
        },
      )
    );
  }
}
